home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 0.9.1.3 stable / flock-0.9.1.3.en-US.win32.exe / flock / components / flockLoggingFileWriter.js < prev    next >
Text File  |  2007-10-12  |  6KB  |  183 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const CLASS_ID                = Components.ID("{b2045e5e-c742-4dbc-8fae-233f428bc9c0}");
  20. const CLASS_NAME              = "Flock Logging File Writer";
  21. const CONTRACT_ID             = "@flock.com/logging-file-writer;1";
  22.  
  23. const LOGGER_FILENAME         = "log.txt"
  24.  
  25. function flockLoggingFileWriter()
  26. {
  27.   this._initialized = false;
  28.   
  29.   var obs = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  30.   obs.addObserver(this, "profile-after-change", false);
  31. }
  32.  
  33. flockLoggingFileWriter.prototype = {
  34.  
  35.   _initialized: null,
  36.  
  37.   _initFile: function()
  38.   {
  39.     // Prepare the log file for the requested module
  40.     var dirService = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
  41.     var profileDir = dirService.get('ProfD', Components.interfaces.nsILocalFile);
  42.     var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  43.     file.initWithPath(profileDir.path);
  44.     file.append(LOGGER_FILENAME);
  45.     if(!file.exists()) file.createUnique(0,0600);
  46.  
  47.     var transport = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  48.     transport.init(file, 0x04 | 0x08 | 0x10, 064, 0);
  49.     
  50.     this._converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  51.     this._converter.charset = "UTF-8";
  52.     
  53.     this._outputStream = Components.classes["@mozilla.org/network/buffered-output-stream;1"].createInstance(Components.interfaces.nsIBufferedOutputStream);
  54.     this._outputStream.init(transport, 65536 * 4);
  55.  
  56.     this._initialized = true;
  57.   },
  58.  
  59.   emit: function(aDate, aLevel, aContext, aMessage)
  60.   {
  61.     if (this._initialized) {
  62.           var levels = new Array("all","debug", "info", "warn", "error", "fatal");
  63.           
  64.           var date = new Date(aDate);
  65.           var dateString = date.getFullYear() + "-" + this._pad(date.getMonth() + 1, 2) + "-" + this._pad(date.getDate(), 2) + " " + this._pad(date.getHours(), 2) + ":" + this._pad(date.getMinutes(), 2) + ":" + this._pad(date.getSeconds(), 2) + "." + this._pad(date.getTime() % 1000, 3);
  66.         var content = "[" + dateString + " " + aContext + ":" + levels[aLevel] + "] " + aMessage + "\n";
  67.         
  68.         var inputStream = this._converter.convertToInputStream(content);
  69.     
  70.         this._outputStream.writeFrom(inputStream, inputStream.available());
  71.         this._outputStream.flush();
  72.       }
  73.   },
  74.  
  75.   _pad: function(aNumber, aPlaces)
  76.   {
  77.     var numberString = aNumber + "";
  78.     while (numberString.length < aPlaces) {
  79.       numberString = "0" + numberString;
  80.     }
  81.     return numberString;
  82.   },
  83.  
  84.   // nsIObserver
  85.   observe: function(subject, topic, state)
  86.   {
  87.     switch (topic) {
  88.       case "profile-after-change":
  89.         // init log file
  90.         this._initFile();
  91.         break;
  92.     }
  93.   },
  94.  
  95.   // nsIClassInfo
  96.   getInterfaces: function(aCount)
  97.   {
  98.     var interfaces = [Components.interfaces.flockILoggingObserver, Components.interfaces.nsIClassInfo];
  99.     aCount.value = interfaces.length;
  100.     return interfaces;
  101.   },
  102.  
  103.   // nsIClassInfo
  104.   getHelperForLanguage: function(aLanguage)
  105.   {
  106.     return null;
  107.   },
  108.  
  109.   // nsIClassInfo
  110.   contractID: CONTRACT_ID,
  111.  
  112.   // nsIClassInfo
  113.   classDescription: CLASS_NAME,
  114.  
  115.   // nsIClassInfo
  116.   classID: CLASS_ID,
  117.  
  118.   // nsIClassInfo
  119.   implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  120.  
  121.   // nsIClassInfo
  122.   flags: Components.interfaces.nsIClassInfo.SINGLETON,
  123.   
  124.   // nsISupports
  125.   QueryInterface: function(aIID)
  126.   {
  127.     if (!aIID.equals(Components.interfaces.nsISupports) && !aIID.equals(Components.interfaces.flockILoggingObserver) && !aIID.equals(Components.interfaces.nsIObserver) && !aIID.equals(Components.interfaces.nsIClassInfo))
  128.       throw Components.results.NS_ERROR_NO_INTERFACE;
  129.     return this;
  130.   }
  131.  
  132. };
  133.  
  134. /******************************************************************************
  135.  * XPCOM Functions for construction and registration
  136.  ******************************************************************************/
  137. var Module = {
  138.   _firstTime: true,
  139.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  140.   {
  141.     if (this._firstTime) {
  142.       this._firstTime = false;
  143.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  144.     }
  145.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  146.     aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  147.     
  148.     // Make the Logging Observer a default
  149.     var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
  150.       .getService(Components.interfaces.nsICategoryManager);
  151.     categoryManager.addCategoryEntry("flockILoggingObserver", CLASS_NAME, CONTRACT_ID, true, true, null);
  152.   },
  153.  
  154.   unregisterSelf: function(aCompMgr, aLocation, aType)
  155.   {
  156.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  157.     aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  158.   },
  159.   
  160.   getClassObject: function(aCompMgr, aCID, aIID)
  161.   {
  162.     if (!aIID.equals(Components.interfaces.nsIFactory))
  163.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  164.     if (aCID.equals(CLASS_ID))
  165.       return Factory;
  166.     throw Components.results.NS_ERROR_NO_INTERFACE;
  167.   },
  168.  
  169.   canUnload: function(aCompMgr) { return true; }
  170. };
  171.  
  172. var Factory = {
  173.   createInstance: function(aOuter, aIID)
  174.   {
  175.     if (aOuter != null)
  176.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  177.     return (new flockLoggingFileWriter()).QueryInterface(aIID);
  178.   }
  179. };
  180.  
  181. function NSGetModule(aCompMgr, aFileSpec) { return Module; }
  182.  
  183.